import { CHANNEL, CVERSION, PROJECTNAME } from './globalData'
import { getStorageSync, showToast, navigateTo } from './handlers'

const globalData = getApp().globalData;

/**
 * @author Dom
 * @description: 请勿在此处使用箭头函数定义方法。
 * 如果你不知道为什么, 这么简单的问题, 有时间也不会告诉你。
 */
const instanceDefaultHandler = {
    bindTapTabItem: function (e) {
        const { tabtype } = e.currentTarget.dataset;
        let url = tabtype === '300' ? '/pages/user/user' : '/pages/index/index';
        navigateTo({ url }, { tabType: tabtype })
    },
    bindgetphonenumber: function (e) {
        const { detail } = e;
        const { encryptedData, iv } = detail;
        if (encryptedData && iv) {
            this.setData({
                hadAuth: true
            })
        } else {
            this.setData({
                hadAuth: false
            })
        }
    },
    toggleAdd2Mine: function (e) {
        this.setData({
            isShowAdd2Mine: !this.data.isShowAdd2Mine
        })
    },
    /**
     * @author Dom
     * @param showmodalattr, type: string
     * state: 取值范围: isShowAddClassifyFirst /
     * @description: 切换弹窗显示状态
     */
    bindToggleModal: function (e) {
        const dataset = e.currentTarget.dataset;
        const { showmodalattr } = dataset;
        let showmodalattrArray = showmodalattr.split(',').map(item => item.trim());
        let resultObject = {};
        showmodalattrArray.map(item => resultObject[item] = !this.data[item]);
        this.setData(resultObject);
        if (showmodalattrArray[2] == "isShowTextarea") {
            const isShowTextarea = showmodalattrArray[2];
            this.setData({
                isShowTextarea: true
            })
        }
      if (showmodalattrArray[1] == "isShowTextarea") {
        const isShowTextarea = showmodalattrArray[2];
        this.setData({
          isShowTextarea: false
        })
      }
    },
    bindInput: function (e) {
        const dataset = e.currentTarget.dataset;
        const { inputkey } = dataset;
        const { value } = e.detail;
        this.setData({
            [inputkey]: value
        });
    },
    /**
     * @author Dom
     * @param type, string
     * state: 用于说明选择文件的类型, image/file
     * @param setting, type: object
     * state: 参照小程序对应API文档。
     * @return:
     * @description:
     */
    bindChooseFile: function (setting, type) {
        type = type || 'image';
        switch (type) {
            case 'image':
                wx.chooseImage({
                    count: 1,
                    ...setting,
                    success: res => {
                        this.bindUpload({
                            filePath: res.tempFilePaths[0],
                            storagePath: setting.storagePath,
                            uploadSuccess: setting.uploadSuccess
                        })
                    }
                });
                break;
        }
    },
    bindUpload: function (setting) {
        wx.uploadFile({
            header: {
                sessionId: globalData.loginInfo.sessionId
            },
            url: `${PROJECTNAME}/restaurant-user/file/upload`,
            name: 'file',
            filePath: setting.filePath,
            formData: {
                cversion: CVERSION,
                channel: CHANNEL,
                smartUserId: globalData.loginInfo.loginedUser.userId,
                storagePath: setting.storagePath
            },
            success: res => {
                const { data = { code: '500', msg: 'data is no define edited by Dom.' } } = res;
                const result = typeof data === 'string' ? JSON.parse(data) : data;
                const { code, msg } = result;
                if (code === 6666) {
                    if (typeof setting.uploadSuccess === 'function') {
                        setting.uploadSuccess(result.data.uploadedList)
                    } else {
                        showToast({ title: 'setting.uploadSuccess show be a function' })
                    }
                } else {
                    showToast({ title: msg })
                }
            },
            fail: res => {
                showToast({
                    title: '文件上传失败'
                })
            }
        })
    }
};

const instanceDefaultData = {
    hadAuth: false,
    isShowAdd2Mine: true
};

module.exports = {
    instanceDefaultHandler,
    instanceDefaultData
}